"""
AppSettings

Settings interface of TheHat application. Bases kivy.uix.settings.Settings.
When AppSettings are going to be closed, they will automatically close opened
popups and signal if they are ready to close.
"""
# -*- coding: utf-8 -*-

from kivy.logger import Logger
from kivy.uix.settings import Settings
from constants import POPUP_TYPE


class AppSettings(Settings):
    def __init__(self, **kwargs):
        super(AppSettings, self).__init__(**kwargs)

    def on_close(self):
        """ Fired when trying to close settings

            Does:
                checks if there were popups opened, if yes, closes them and
                blocks settings' closing
            Returns:
                (bool) if settings should or shouldn't be closed
        """
        close_settings = True

        for child in self.parent.children:
            if isinstance(child, POPUP_TYPE):
                Logger.debug('Dismissing popup.')
                child.dismiss()
                close_settings = False

        return close_settings
